import { getWangEditor } from '@/api/other'
export function getFileType(fileName){
  return fileName.substring(fileName.lastIndexOf('.') + 1);
}
export function getArrayToHis(data) {
  // 先用map结构记录
  function arrayToMap(data) {

    // 非数组或数据长度为0不处理
    if (!(data instanceof Array) || data.length === 0) {
      return {}
    }
    
    var map = {}
    for (var i = 0; i < data.length; i++) {
      //
      // if (!data[i] instanceof Array || data.length < 2) {
        //  1 不是一个对象 2 第一项没得title 跳过
        if (!(data[i] instanceof Object)|| !data[0].title) {
          continue
        }
        const obj = data[i]
        // console.log(obj, 'obj')
        // obj.title//
        const name = 'n_' + obj.width + '_' + obj.height + '_' + obj.distLeft + '_' + obj.distTop
      //
      if (name !== undefined) {
        if (map[name] === undefined) {
          map[name] = []
        }
        map[name].push(data[i])
      }
    }

    return map
  }

  // 把map转成数组
  function mapToArray(data) {
    if (data == undefined) {
      return []
    }
    const array = [] as any
    for (const p in data) {
      array.push(data[p])
    }
    return array
  }
  const obj = arrayToMap(data)
  return mapToArray(obj)
}
export function getArrayToHis1(data) {
  // 先用map结构记录
  function arrayToMap(data) {

    // 非数组或数据长度为0不处理
    if (!(data instanceof Array) || data.length === 0) {
      return {}
    }
    
    var map = {}
    for (var i = 0; i < data.length; i++) {
      //
      // if (!data[i] instanceof Array || data.length < 2) {
        //  1 不是一个对象 2 第一项没得title 跳过
        if (!(data[i] instanceof Object)) {
          continue
        }
        const obj = data[i]
        // console.log(obj, 'obj')
        // obj.title//
        const name = 'n_' + obj.width + '_' + obj.height + '_' + obj.distLeft + '_' + obj.distTop
      //
      if (name !== undefined) {
        if (map[name] === undefined) {
          map[name] = []
        }
        map[name].push(data[i])
      }
    }

    return map
  }

  // 把map转成数组
  function mapToArray(data) {
    if (data == undefined) {
      return []
    }
    const array = [] as any
    for (const p in data) {
      array.push(data[p])
    }
    return array
  }
  const obj = arrayToMap(data)
  return mapToArray(obj)
}
// 处理富文本数据
export const wandEditorDate = async (data) => {
  if (!data) return
  const newPath = await getImage(data)
  //  
  if (!newPath.length) return data
  // 对内容进行处理
  const imgRegex = /<img.*?>/gi;
  let srcIndex = -1
  return data.replace(imgRegex, (item, item1) => {
    // 需要替换数据
    let srcReg = /src="(.*?)"/g; // 匹配 img 标签中的 src 属性
    return item.replace(srcReg, match => {
      srcIndex++
      return `src="${newPath[srcIndex]}"`
    })
  })
}
export const getImage = async (data) => {
  // 1数据 2 组合成数组 3 请求 4 返回数据url
  let imgHref = [] as any
  let imgData = [] as any
  const regImage = /<img\b[^>]*>/gi;
  const href = /data-href="([^"]*)"/
  data.replace(regImage, (match: any) => {
    const item = match.match(href);
    if (item) {
      const dataHref = item[1];
      imgHref.push(dataHref)
    } else {
      console.log('data-href attribute not found');
    }
    return match
  });
  // 循环请求数据
  if (imgHref.length > 0) {
    for (var i = 0; i < imgHref.length; i++) {
      const imgDate: any = await getWangEditor(imgHref[i])
      const src = await window.webkitURL.createObjectURL(new Blob([imgDate], { type: 'image/png' }))
      imgData.push(src)
    }
  }
  return imgData
}
// 文件下载
export const getDownFile=(data,name)=>{
  let url = window.URL.createObjectURL(new Blob([data as any]))
      let link = document.createElement('a')
      link.style.display = 'none'
      link.href = url
      link.setAttribute('download', name)
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link);
}